package scuts.core;

import scuts.core.Map;
import scuts.core.types.ProgressiveFuture;
import utest.Assert;
import scuts.core.types.Option;
using scuts.core.Map;
using scuts.core.extensions.Options;

class MapTest 
{
  public function new() 
  {
    
  }
  
  public function testOption() 
  {
    var o = Some(5);
    var last = o.map(function (i) return i + 2);
    
    
    Assert.equals(7, last.value());

  }
  
  public function testFuture() 
  {
    var f = new ProgressiveFuture<Int>();
    
    var last = f.map(function (i) return i + 2);
    
    var res = -1;
    last.deliverTo(function (r) res = r);
    
    f.deliver(3);
    
    Assert.equals(5, res);

  }
  
  public function testIterator() 
  {
    var iter = [1, 2].iterator();
    var func = function (i) return i+1;
    
    var res = iter.map(func);
    
    Assert.isTrue(res.hasNext());
    Assert.same(2, res.next());
    Assert.isTrue(res.hasNext());
    Assert.same(3, res.next());
    Assert.isFalse(res.hasNext());

  }
  
  public function testLazyIterator() 
  {
    var iter = function () return [1, 2].iterator();
    var func = function (i) return i+1;
    
    var lazy = iter.map(func);
    var res = lazy();
    
    Assert.isTrue(res.hasNext());
    Assert.same(2, res.next());
    Assert.isTrue(res.hasNext());
    Assert.same(3, res.next());
    Assert.isFalse(res.hasNext());
    
    var res = lazy();
    
    Assert.isTrue(res.hasNext());
    Assert.same(2, res.next());
    Assert.isTrue(res.hasNext());
    Assert.same(3, res.next());
    Assert.isFalse(res.hasNext());
        
    
  }
  
  public function testLazyIteratorWithInfiniteLength() 
  {
    
    var iter = function () {
      var i = 0; 
      return { hasNext : function () return true, next : function () return i++ }
    }
    
    var func = function (i) return i+1;
    
    var lazy = iter.map(func).map(func).map(func);
    var res = lazy();
    
    Assert.isTrue(res.hasNext());
    Assert.same(3, res.next());
    Assert.isTrue(res.hasNext());
    Assert.same(4, res.next());
    
    
    var res = lazy();
    
    Assert.isTrue(res.hasNext());
    Assert.same(3, res.next());
    Assert.isTrue(res.hasNext());
    Assert.same(4, res.next());
    
        
    
  }
  
}